

{extend name="common/form" /}
{block name="form" /}
<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
    <div class="widget am-cf">
        <div class="widget-head am-cf">

            <div class="widget-title am-fl">修改数据</div>
            <div class="widget-function am-fr">
                <a href="javascript:;" class="am-icon-cog"></a>
            </div>
        </div>
        <div class="widget-body am-fr">
            <form class="am-form tpl-form-border-form tpl-form-border-br">
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        昵称
                    </label>
                    <div class="am-u-sm-9">
                        <input type="text" name="nickname" value="{notempty name='$info.nickname'}{$info.nickname}{/notempty}" required="required" class="tpl-form-input" placeholder="请填写昵称">
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        用户角色
                    </label>
                    <div class="am-u-sm-9">
                        <select name="role_id" required="required" data-am-selected="{searchBox: 2}">
                            {foreach $role as $v}
                            <option value="{$v.id}" {notempty name='$info.role_id'}{eq name="$info.role_id" value="$v.id"}selected{/eq}{/notempty}>{$v.name}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        用户名                    </label>
                    <div class="am-u-sm-9">
                        <input type="text" name="username" {notempty name='$info.username'} readonly{/notempty} value="{notempty name='$info.username'}{$info.username}{/notempty}" required="required" class="tpl-form-input" placeholder="请填写用户名">
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        密码                    </label>
                    <div class="am-u-sm-9">
                        <input type="password" name="password"  required="required" class="tpl-form-input" placeholder="请填写密码">
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        确认密码
                    </label>
                    <div class="am-u-sm-9">
                        <input type="password" name="to_password"  required="required" class="tpl-form-input" placeholder="请填写密码">
                    </div>
                </div>

                <!--<div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        头像                    </label>
                    <div class="am-u-sm-9">
                    </div>
                </div>-->
                {notempty name='$info.id'}
                <input type="hidden" name="headlike" value="{notempty name='$info.headlike'}{$info.headlike}{/notempty}"  class="tpl-form-input" placeholder="请填写头像">
                <input type="hidden" name="id" value="{notempty name='$info.id'}{$info.id}{/notempty}" required="required" class="tpl-form-input" placeholder="请填写头像">
                {/notempty}
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        是否是超级管理员                    </label>
                    <div class="am-u-sm-9">
                        <div class="tpl-switch">
                            <input type="checkbox" name="is_supper"  class="ios-switch bigswitch tpl-switch-btn am-margin-top-xs" {notempty name='$info.is_supper'}{eq name='$info.is_supper' value="1"}checked="checked"{/eq}{/notempty}>
                            <div class="tpl-switch-btn-view">
                                <div></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        是否锁定
                    </label>
                    <div class="am-u-sm-9">
                        <div class="tpl-switch">
                            <input type="checkbox" name="lock"  class="ios-switch bigswitch tpl-switch-btn am-margin-top-xs" {notempty name='$info.lock'}{eq name='$info.lock' value="1"}checked{/eq}{/notempty}>
                            <div class="tpl-switch-btn-view">
                                <div></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="am-form-group">
                    <div class="am-u-sm-9 am-u-sm-push-3">
                        <button type="button" onclick="addDatas('{:url($type)}','form')" class="am-btn am-btn-primary tpl-btn-bg-color-success" >
                            提交
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    function addDatas(url,form) {

        var data = $(form).serializeArray();
        var dataString = '{';
        for(var j=0;j<data.length;j++){
            if(data[j].name.charAt(data[j].name.length-1) !=']'){
                var obj = $('input[name="'+data[j].name+'"]').eq(0)
                var required =obj.attr('required');
                if(!required){
                    obj =  $('select[name="'+data[j].name+'"]').eq(0)
                }
                if(required == 'required'){
                    if(!obj.val()){
                        var name = obj.parent('div').prev('label').html();
                        layer.msg(name+" 不能为空！");
                        return true;
                    }
                }
                dataString+='"'+data[j].name+'":"'+data[j].value+'",';
            }
        }
        dataString = dataString.substring(0,dataString.length - 1);
        dataString+="}";
        var dataObj = JSON.parse(dataString);
        if(dataObj.password != dataObj.to_password){
           return layer.alert('两次密码输入不一致',{icon:5})
        }
        var pat = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z\\W]{6,18}$/;
        if(!pat.test(dataObj.password)){
            return layer.alert(' 密码格式必须是数字字母组合！',{icon:5})
        }


        dataObj.password = sha1(dataObj.password);
        dataObj.to_password = sha1(dataObj.to_password);
        dataObj.is_supper =dataObj.is_supper =='on'?1:0;
        $.post(url,dataObj,function (res) {
            if(res.code!=0){
                layer.alert(res.msg,{icon:5})
            }else{
                layer.alert('操作成功!',{icon:6},function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    window.parent.location.reload();
                    parent.layer.close(index);//关闭当前页

                })
            }
        })


        $.myAjax(url,{old_password:sha1($('#p').val()),password:sha1($('#p1').val()),to_password:sha1($('#p2').val())},function (res) {
            layer.alert('操作成功!',{icon:6},function () {
                var index = parent.layer.getFrameIndex(window.name);
                window.location.reload();
                layer.close(index);//关闭当前页
            })
        })
    }

</script>
{/block}
{extend}
